<template>
    <div class="createPost-container" style="margin: 10px">
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="基础设置" name="first">
                <el-form ref="postForm" :model="postForm" :rules="rules" class="form-container">
                    <div class="createPost-main-container">
                        <el-form-item prop="name" style="margin-bottom: 40px;" label-width="90px" label="平台名称:">
                            <el-input v-model="postForm.name" :rows="1" class="article-textarea" autosize placeholder="请输入平台名称" />
                        </el-form-item>
                        <el-button v-loading="loading" style="margin-left: 10px;" type="primary" @click="submitForm">
                            保存
                        </el-button>
                    </div>
                </el-form>
            </el-tab-pane>
            <el-tab-pane label="备案" name="beian">
                <Beian />
            </el-tab-pane>
            <el-tab-pane label="联系" name="contacts">
                <Contacts />
            </el-tab-pane>
        </el-tabs>
    </div>
</template>

<script>
import { fetch, update } from '@/platform/api/conf/conf'

import Beian from '@/platform/components/conf/Beian'
import Contacts from '@/platform/components/conf/Contacts'

const defaultForm = {
    name: ''
}

export default {
    name: 'Conf',
    components: { Beian, Contacts },
    data() {
        const validateRequire = (rule, value, callback) => {
            if (value === '') {
                this.$message({
                    message: rule.message,
                    type: 'error'
                })
                callback(new Error())
            } else {
                callback()
            }
        }
        return {
            i: 0,
            activeName: 'first',
            postForm: Object.assign({}, defaultForm),
            typeList: [],
            loading: false,
            userListOptions: [],
            rules: {
                name: [{ validator: validateRequire, message: '平台名称为必填字段', trigger: 'blur' }]
            },
            tempRoute: {}
        }
    },
    created() {
        this.fetchData()
    },
    methods: {
        handleClick(tab, event) {
            // console.log(tab, event)
        },
        changeTypeSelect() {

        },
        fetchData() {
            fetch().then(response => {
                this.postForm.name = response.data.conf.name
            }).catch(err => {
                console.log(err)
            })
        },
        submitForm() {
            this.$refs.postForm.validate(valid => {
                if (valid) {
                    this.loading = true
                    update(this.postForm).then(response => {
                        this.$notify({
                            title: '成功',
                            message: '修改成功',
                            type: 'success',
                            duration: 2000
                        })
                        // this.postForm.status = 'published'
                        this.loading = false
                        // this.$router.push('/store/index')
                    }).catch(err => {
                        console.log(err)
                        this.loading = false
                    })
                }
            })
        },
        draftForm() {
            if (this.postForm.content.length === 0 || this.postForm.title.length === 0) {
                this.$message({
                    message: '请填写必要的标题和内容',
                    type: 'warning'
                })
                return
            }
            this.$message({
                message: '保存成功',
                type: 'success',
                showClose: true,
                duration: 1000
            })
            this.postForm.status = 'draft'
        }
    }
}
</script>

<style lang="scss" scoped>
.createPost-container {
    position: relative;

    .createPost-main-container {
        padding: 40px 45px 20px 50px;
    }
}
</style>
